package com.tarena.lbs.user.service.impl;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.user.dao.mapper.AdminMapper;
import com.tarena.lbs.user.pojo.param.AdminParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public AdminVO detail() {
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        log.debug("用户信息：{}", userPrinciple);
        Integer id = userPrinciple.getId();
        AdminPO adminPO = adminMapper.selectById(id);
        AdminVO adminVO = new AdminVO();
        BeanUtils.copyProperties(adminPO, adminVO);
        return adminVO;
    }

    @Override
    public PageResult<AdminVO> pageList(AdminQuery query) {
        return null;
    }

    @Override
    public void save(AdminParam param) throws BusinessException {
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        if (userPrinciple == null) {
            throw new BusinessException("-2", "用户认证解析失败");
        }
        Roles loginRole = userPrinciple.getRole();
        if (loginRole != Roles.ADMIN) {
            throw new BusinessException("-2", "用户角色权限不足");
        }

    }
}
